import {getUrlParam} from '@/static/js/filter.js'
export default {
	data() {
		return {
			appConstitutionEntityVo:[],  //从缓存中获取
			submitAppConstitutionEntityVo:[], //点击提交
			calculatePoints: {},
			defaultArr:['平和质','气虚质','阳虚质','阴虚质','痰湿质','湿热质','血瘀质','气郁质','特禀质'],
			id:'',
			calculateStatus:false,
			currentIndex: 9,
			showModal:false,  //弹框
			showContent:'',
			signData: '',
		}
	},
	created() {
		const _this = this;
		if(!getUrlParam('readstatus')) {
			console.log(11)
			const id = getUrlParam('editId')
			_this.signData = getUrlParam('signData');
			_this.$api.findByConstitutionId(id).then((res)=>{
				if(res.data.returnCode == '200') {
					_this.calculatePoints = res.data.result.appConstitutionEntity;
					if(_this.calculatePoints.isAmended == 2) {
						_this.calculateStatus = true;
					}
				}
			})
			_this.appConstitutionEntityVo = localStorage.getItem('questionData') && JSON.parse(localStorage.getItem('questionData')).map((a) => {
				var bresult = a.data.map((b) => {
					var cresult = []
					cresult = b.options.filter((c) => {
						c.label = a.id
						return c.checked2
					})
					return cresult[0]
				})
				
				bresult = bresult.map((d)=>{
					return {
						label:d && d.label || '',
						healthOption:d && d.userOption || '',
						healthParame:d && d.userParame || '',
						orde: 0,
						id:d && d.id || ''
					}
				})
				return bresult
			})
			var obj = []
			_this.appConstitutionEntityVo && _this.appConstitutionEntityVo.map((item,index) => {
				obj = obj.concat(item);
			});
			_this.submitAppConstitutionEntityVo = {
				appConstitutionQuestionnaireEntities: obj,
				userId: Number(localStorage.userId),
				// type: 2,
				healthId: Number(localStorage.healthId),
			}
		} else if(getUrlParam('readstatus')) {
			const id = getUrlParam('editId')
			this.$api.findByConstitutionId(id).then((res) => {
				if(res.data.returnCode == '200') {
					this.calculatePoints = res.data.result.appConstitutionEntity;
					if(this.calculatePoints.isAmended == 2) {
						this.calculateStatus = true;
					}
				}
			})
		}
	},
	methods: {
		 // 点击tab返回到上一页指定的位置
		back(e) {
			this.currentIndex = e;
			const editId = getUrlParam('editId') ? getUrlParam('editId') : '';
			const userId = getUrlParam('editId') ? getUrlParam('userId') : '';
			const status = getUrlParam('status') ? getUrlParam('status') : '';
			if(editId && userId && !this.signData) {
				uni.navigateTo({
					url:'./questionnaireDetailTCM?current='+e+'&editId='+editId+'&id='+userId+'&status='+status+'&sex='+getUrlParam('sex')
				})
			} else if(editId && userId && this.signData) {
				uni.navigateTo({
					url:'./questionnaireDetailTCM?current='+e+'&editId='+editId+'&id='+userId+'&status='+status+'&sex='+getUrlParam('sex')+'&signData='+this.signData
				})
			}else if(editId && getUrlParam('readstatus')){
				uni.navigateTo({
					url:'./questionnaireDetailTCM?current='+e+'&status='+status+'&sex='+getUrlParam('sex')+'&editId='+editId+'&readstatus=1'
				})
			} else if (editId && !getUrlParam('readstatus')) {
				uni.navigateTo({
					url:'./questionnaireDetailTCM?current='+e+'&status='+status+'&sex='+getUrlParam('sex')+'&editId='+editId
				})
			}
			
		},
		// 点击计算转化分
		calculate() {
			const _this = this;
			if(getUrlParam('status') == 2 || getUrlParam('readstatus') == 1){
				uni.showToast({title:'已签发',icon:'none'});
				return;
			}			
			if(this.signData) {
				uni.showToast({title:'该问卷已签发',icon:'none'});
				return;
			}
			_this.submitAppConstitutionEntityVo.appConstitutionQuestionnaireEntities.map((a)=>{
				console.log(JSON.stringify(a))
			})
			// return;
			_this.submitAppConstitutionEntityVo.healthId = JSON.parse(localStorage.userInfo).id;
			_this.submitAppConstitutionEntityVo.userId = Number(getUrlParam('userId'));
			_this.submitAppConstitutionEntityVo.id = Number(getUrlParam('editId'));
			// return console.log(_this.submitAppConstitutionEntityVo)
			_this.$api.AppConstitutionQuestionnaireUpdate(_this.submitAppConstitutionEntityVo).then((res) => {
				if(res.data.returnCode == '200') {
					localStorage.removeItem('questionData');
					_this.calculatePoints = res.data.result; 
					 if(_this.calculatePoints.isAmended == 2) {
						_this.calculateStatus = true;
					 }
					
					
				}
			})
		},
		// 弹框确认
		Modalconfirm() {
			this.showModal = false; //弹框
		},
		// 返回上一页
		page() {
			uni.navigateBack({
				delta: 1
			});
		},
		// 返回列表
		goList() {
			uni.navigateBack({
				delta:2
			})
		},
		// 截取时间
		shijiantime(times){
			var timearr = times.replace(" ", ":").replace(/\:/g, "-").split("-");
			var timestr = ""+timearr[0]+"-" + timearr[1] + "-" + timearr[2]
			return timestr
		},
		operation() {
			uni.navigateTo({
				url:'/pages/quickly/tcm/createTcm?sex='+getUrlParam('sex')
			})
		}
	}
}